# encoding: utf-8
__author__ = 'binbin'
__date__ = '2018/10/18 0013 16:25'


from rest_framework import serializers
from rest_framework.validators import UniqueTogetherValidator
from .models import UserFav,UserLeavingMessage,UserAddress
from goods.serializers import GoodsSerializer


class UserFavDetaiaSerializer(serializers.ModelSerializer):
    # 通过goods_id拿到商品信息
    goods = GoodsSerializer

    class Meta:
        model = UserFav
        fields = ('goods','id')


class UserFavSerializer(serializers.ModelSerializer):
    user = serializers.HiddenField(default=serializers.CurrentUserDefault())

    class Meta:
        model = UserFav

        # 使用validate方式实现唯一联合
        validate =[
            UniqueTogetherValidator(
                queryset=UserFav.objects.all(),
                fields=('user','goods'),
                message='已经收藏'
            )
        ]
        fields = ('user','goods','id')


class LeavingMessageSerializer(serializers.ModelSerializer):
    user = serializers.HiddenField(default=serializers.CurrentUserDefault())

    add_time = serializers.DateTimeField(read_only=True,format='%Y-%m-%d %H:%M')

    class Meta:
        model = UserLeavingMessage
        fields = ('user','message_type','subject','message','file','id','add_time')


class AddressSerializer(serializers.ModelSerializer):
    user = serializers.HiddenField(default=serializers.CurrentUserDefault())
    add_time = serializers.DateTimeField(read_only=True,format='%y-%m-%d %H:%M')

    class Meta:
        model = UserAddress
        fields = ('id','user','province','city','district','address','signer_name','add_time','signer_mobile')

        